package models

import (
	"chinasoccer/pkg/utils"
	"fmt"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// 采购清单号 员工编号 采购数量 采购时间 采购总价 备注
// MasterID WorkerID PurchaseNum PurchaseTime PurchaseTotle PurchaseOthers

type PurchaseMasterTable struct {
	gorm.Model
	MasterID       string `json:"masterid" form:"masterid" gorm:"type:varchar(20);not null;unique;" binding:"required"`
	WorkerID       string `json:"workerid" form:"workerid" gorm:"type:varchar(20);not null;" binding:"required"`
	PurchaseNum    string `json:"purchasenum" form:"purchasenum" gorm:"type:varchar(20);not null;" binding:"required"`
	PurchaseTime   string `json:"purchasetime" form:"purchasetime" gorm:"type:varchar(20);not null" binding:"required"`
	PurchaseTotle  string `json:"purchasetotle" form:"purchasetotle" gorm:"type:varchar(20);not null;" binding:"required"`
	PurchaseOthers string `json:"purchaseothers" form:"purchaseothers" gorm:"type:varchar(255);not null;" binding:"required"`
	IsDel          bool   `json:"isdel" form:"isdel" gorm:"type:boolean default false;"`
}

func getJson3(c *gin.Context) PurchaseMasterTable {
	json := make(map[string]string, 1024)
	c.ShouldBind(&json)
	masterID := json["masterid"]
	workerID := json["workerid"]
	purchaseNum := json["purchasenum"]
	purchaseTime := json["purchasetime"]
	purchaseTotle := json["purchasetotle"]
	purchaseOthers := json["purchaseothers"]
	purchaseMasterTable := PurchaseMasterTable{
		MasterID:       masterID,
		WorkerID:       workerID,
		PurchaseNum:    purchaseNum,
		PurchaseTime:   purchaseTime,
		PurchaseTotle:  purchaseTotle,
		PurchaseOthers: purchaseOthers,
	}
	return purchaseMasterTable
}

// 获取所有的 PurchaseMasterTables
func GetAllPurchaseMasterTable(purchaseMasterTableParam map[string]interface{}) ([]PurchaseMasterTable, int64, error) {
	var purchaseMasterTableData []PurchaseMasterTable
	pagenum := purchaseMasterTableParam["pagenum"].(int)
	pagesize := purchaseMasterTableParam["pagesize"].(int)
	query := purchaseMasterTableParam["query"].(string)
	if query == "" {
		var total int64
		err := db.Table("purchase_master_tables").Where("is_del = false").Count(&total).Error
		if err != nil {
			return purchaseMasterTableData, 0, err
		}
		err = db.Table("purchase_master_tables").Where("is_del = false").Offset((pagenum - 1) * pagesize).Limit(pagesize).Find(&purchaseMasterTableData).Error
		fmt.Println("total = ", total)
		return purchaseMasterTableData, total, err
	}
	purchaseName := query

	var purchase PurchaseMasterTable
	err := db.Table("purchase_master_tables").Where("master_id = ?", purchaseName).First(&purchase).Error
	purchaseMasterTableData = append(purchaseMasterTableData, purchase)
	if err != nil {
		return purchaseMasterTableData, 0, err
	}

	return purchaseMasterTableData, 1, err
}

// 增添采购主表
func AddPurchaseMasterTable(c *gin.Context) (PurchaseMasterTable, int64, error) {
	purchase := getJson3(c)
	var total int64
	db.Table("purchase_master_tables").Where("master_id = ?", purchase.MasterID).Count(&total)
	if total == 0 {
		err := db.Table("purchase_master_tables").Create(&purchase).Error
		return purchase, total, err
	}
	return purchase, total, nil
}

// 更新采购主表
func UpdatePurchaseMasterTable(c *gin.Context) (PurchaseMasterTable, error) {
	purchase := getJson3(c)
	err := db.Table("purchase_master_tables").Where("master_id = ?", purchase.MasterID).Updates(&purchase).Error
	return purchase, err
}

// 删除采购主表
func DelPurchaseMasterTable(c *gin.Context) error {
	id := c.Params.ByName("id")
	err := db.Table("purchase_master_tables").Where("master_id = ?", id).Update("is_del", true).Error
	if err != nil {
		return err
	}
	err = db.Table("purchase_master_tables").Where("master_id = ?", id).Update("master_id", utils.RandomString(19)).Error
	if err != nil {
		return err
	}
	// var purchaseMaster PurchaseMasterTable
	// err = db.Table("purchase_master_tables").Where("master_id = ?", id).First(&purchaseMaster).Error
	// if err != nil {
	// 	return err
	// }
	// err = db.Table("purchase_details").Where("purchase_detail_master_id = ?", purchaseMaster.MasterID).Update("is_del", true).Error
	// if err != nil {
	// 	return err
	// }
	// err = db.Table("purchase_details").Where("purchase_detail_master_id = ?", purchaseMaster.MasterID).Update("purchase_detail_master_id", utils.RandomString(19)).Error
	return err
}
